<?php
defined('BASEPATH') OR exit('No direct script access allowed');


use QCloud_WeApp_SDK\Mysql\Mysql as DB;


class UserLogin extends CI_Controller
{
    public function index()
    {
        $openid = $_POST['openid'];
        $unionid = $_POST['unionid'];
        $nickname = $_POST['nickname'];
        $gender = $_POST['gender'];
        $province = $_POST['province'];
        $city = $_POST['city'];
        $country = $_POST['country'];
        $avatarUrl = $_POST['avatarurl'];

        $language = $_POST['language'];
        $timestamp = $_POST['timestamp'];
        $appid = $_POST['appid'];
        $skey = $_POST['skey'];

        $rows = null;


        if($unionid&&$openid){
            $rows = DB::row('cc_user', ['*'], ['unionid' => $unionid,'openid' => $openid]);
            if(!$rows){
                DB::insert('cc_user', ['openid' => $openid,'unionid' => $unionid, 'nickname' => $nickname,
                    'gender' => $gender, 'province' => $province,
                    'city' => $city, 'country' => $country, 'avatarurl' => $avatarUrl,'language' => $language,'appid' => $appid,'skey' => $skey,'timestamp' => $timestamp]);

                $rows = DB::row('cc_user', ['*'], ['unionid' => $unionid,'openid' => $openid]);
            }
        }



        if (!empty($rows)) {
            $this->json([
                'code' => 0,
                'data' => $rows
            ]);
        } else {
            $this->json([
                'code' => -1,
                'error' => $rows['error']
            ]);
        }
    }
}
